/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from 'antd';
import {
  wxGetSettingList, // 设置，设置列表
  wxOnlinePay,
  wxScoreBind,
  wxOpenScore,
  getScoreDetail,
  saveScore,
  getAdvSetting,
  setAdv,
  getAdvList,
  deleteAdv,
  sortAdv,
  addAdv,
  editAdv,
  changeAdvStatus,
  getServiceInfo,
  editServiceInfo,
  wxChangeSubscribeStatus,
  getUnbindQrCode,
  checkUnBindRecord,
  getMenuList,
  getOfficialMenu,
  getMiniProgramMenu,
  checkHasOpenTCJS,
  publishMenu,
  setRevenueAdvShowPosition,
  getRevenueAdvShowPosition,
  getPromoteMiniProgram,
  editPromoteMiniProgram,
  copySystemURL,
  getMiniAdvList,
  setMiniAdv,
  deleteMiniAdv,
  changeMiniAdvStatus,
} from '@/services/platform';
import { isLegalData } from '@/utils/utils';
import { isArray, isEqual, cloneDeep } from 'lodash';

export default {
  namespace: 'wxSettings',

  state: {
    settingList: [],
    // 广告设置表单初始值
    advSetting: {
      mini_app_show: false,
      official_accounts_show: false,
      mini_app_checkBox: [],
      official_accounts_checkBox: [],
      // 已选中的插屏广告位置
      selectedPostAdv: [],
      // 已选中的福利广告位置
      selectedWelfareAdv: [],
      // 已选中的Banner广告位置
      selectedBannerAdv: [],
    },
    // 广告设置的原始数据
    advSettingFormat: {},
    advList: [],
    // 客服设置的二维码
    servicesQrCode: '',
    // 客服设置的联系电话
    servicePhones: [],
    // 解绑需要的二维码
    unBindQrcode: '',
    unBindSceneId: '',
    menuButtonList: [],
    miniProgramMenuList: [],
    miniProgramMenuMap: {},
    officialMenuList: [],
    officialMenuMap: {},
    showMenuSetting: false, // 是否展示菜单设置
    selectMenuData: {}, // 选中的菜单数据
    currentMiniAppPlatform: {}, // 菜单设置，当前有绑定关系的小程序
    revenueCheckedList: {}, // 菜单设置，收益广告展示选中列表
    updatingMenu: false, // 菜单更新状态
    systemMenuURL: '',
  },

  effects: {
    // 数据中心，获取业务员列表
    *wxGetSettingList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(wxGetSettingList, payload);
      if (!response) return;
      const { code = 0, data = {}, msg } = response || {};
      const {
        pay_status = '2',
        integral_status = '2',
        advertising_status = '1',
        relation_services_status = '2',
        attention_gzh_status = '2',
        custom_menu = '2',
        revenue_advertise = '2',
        promote_miniprogram = '2',
      } = data;
      const list = [
        {
          title: '在线支付',
          status: pay_status,
          key: 'pay',
          info: '运费提现手续费5‰（微信收取，我方代扣）',
        },
        {
          title: '积分功能',
          status: integral_status,
          key: 'score',
          info:
            '1元=100积分（新用户赠送积分/绑定手机号赠送积分/在线支付赠送积分,在线支付运费时可抵扣运费）',
        },
        {
          title: '广告位',
          status: advertising_status,
          key: 'advertisement',
          info: '多渠道广告投放，提升品牌印象，精准获得有效客户流量，流量变现轻轻松松把钱赚！',
        },
        {
          title: '联系客服',
          status: relation_services_status,
          key: 'connect',
          info: '便于用户反馈及时处理',
        },
        {
          title: '关注公众号',
          status: attention_gzh_status,
          key: 'subscribe',
          info:
            '公众号引流，增加粉丝数，流量可变现；给用户推送订单状态、物流信息，快递信息早知道！',
        },
        {
          title: '自定义菜单',
          status: custom_menu,
          key: 'menu',
          info: '设置公众号菜单链接',
        },
        {
          title: '收益广告',
          status: revenue_advertise,
          key: 'revenue',
          info: '开启官方收益广告，客户在定制公众号点击广告可获得广告佣金收入',
        },
        {
          title: '小程序推广',
          status: promote_miniprogram,
          key: 'promotion',
          info: '设置相关功能项优先使用小程序',
        },
      ];

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            settingList: list,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置，是否开启在线支付设置
    *wxOnlinePay({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wxOnlinePay, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，关注公众号设置开启关闭
    *wxChangeSubscribeStatus({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wxChangeSubscribeStatus, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置，开启积分功能
    *wxOpenScore({ payload, __dva_reject }, { call }) {
      const response = yield call(wxOpenScore, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置，积分功能，绑定云店账号
    *wxScoreBind({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wxScoreBind, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置，积分功能，获取积分设置详情
    *wxGetScoreDetail({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getScoreDetail, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;

      if (code == 0) {
        __dva_resolve(isLegalData(data, {}));
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置，积分功能，设置积分
    *wxSaveScore({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(saveScore, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      if (code == 0) {
        message.success('保存成功！');
        __dva_resolve(isLegalData(data, {}));
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，获取广告位设置
    *getAdvSetting({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getAdvSetting, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      if (code == 0) {
        const {
          weixin: { status: official_accounts_check, ...officialRest },
          weixinMini: { status: mini_app_check, ...miniRest },
        } = data;
        /** 过滤生成选中的数组 */
        const filterFun = (checkBoxObj = {}) => {
          const checkBoxValue = [];
          const keyValue = Object.keys(checkBoxObj || {});
          keyValue.forEach(key => {
            if (checkBoxObj[key] == 2) {
              checkBoxValue.push(key);
            }
          });
          return checkBoxValue;
        };

        yield put({
          type: 'save',
          payload: {
            advSetting: {
              mini_app_show: mini_app_check == 2,
              official_accounts_show: official_accounts_check == 2,
              mini_app_checkBox: filterFun(miniRest),
              official_accounts_checkBox: filterFun(officialRest),
            },
            advSettingFormat: data,
          },
        });

        __dva_resolve(isLegalData(data, {}));
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，设置广告位
    *setAdv({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(setAdv, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('设置成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，获取广告位列表
    *getAdvList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getAdvList, payload);
      if (!response) return;
      const { code, data = [], msg } = response;
      const list = isLegalData(data);
      list.forEach((v, index) => {
        v.sort = index + 1;
      });
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            advList: list,
          },
        });
        __dva_resolve(isLegalData(data));
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，删除广告位
    *deleteAdv({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(deleteAdv, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('删除成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，广告位排序
    *sortAdv({ __dva_resolve, __dva_reject }, { call, select }) {
      const { advList = [] } = yield select(_ => _.wxSettings);
      const payload = [];
      advList.forEach(({ id }, index) => {
        payload.push({
          id,
          sort: index + 1,
        });
      });

      const response = yield call(sortAdv, { sort: JSON.stringify(payload) });
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('排序成功');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，新增广告位
    *addAdv({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addAdv, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('添加成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，修改广告位
    *editAdv({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editAdv, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('修改成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，更改广告位状态
    *changeAdvStatus({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(changeAdvStatus, payload);
      const { status } = payload;
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(`${status == 1 ? '上线' : '下线'}成功！`);
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，获取流量主广告设置列表
    *getMiniAdvList({ payload, __dva_resolve, __dva_reject }, { call, put, select }) {
      const { advSetting = [] } = yield select(_ => _.wxSettings);
      const response = yield call(getMiniAdvList, payload);
      if (!response) return;
      const { code, msg, data = [] } = response;
      if (code == 0) {
        const miniAdvList = isLegalData(data);
        // 已选中的插屏广告位置
        const selectedPostAdv = [];
        // 已选中的福利广告位置
        const selectedWelfareAdv = [];
        // 已选中的Banner广告位置
        const selectedBannerAdv = [];

        miniAdvList.forEach(val => {
          const { type: adv_type, position: adv_pos = [] } = val;
          if (adv_type == 1) {
            selectedBannerAdv.push(...adv_pos);
          }
          if (adv_type == 2) {
            selectedPostAdv.push(...adv_pos);
          }
          if (adv_type == 3) {
            selectedWelfareAdv.push(...adv_pos);
          }
        });

        yield put({
          type: 'save',
          payload: {
            miniAdvList,
            advSetting: {
              ...advSetting,
              selectedPostAdv,
              selectedWelfareAdv,
              selectedBannerAdv,
            },
          },
        });
        __dva_resolve(miniAdvList);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，添加流量主广告
    *addMiniAdv({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(setMiniAdv, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('添加成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，设置流量主广告
    *setMiniAdv({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(setMiniAdv, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('编辑成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，删除流量主广告
    *deleteMiniAdv({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(deleteMiniAdv, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('删除成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，广告位，流量主广告上下线
    *changeMiniAdvStatus({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(changeMiniAdvStatus, payload);
      const { status } = payload;
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(`${status == 1 ? '上线' : '下线'}成功！`);
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，联系客服，获取客服信息
    *getServiceInfo({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getServiceInfo, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { services_qrcode = '', services_phone = [] } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            servicesQrCode: services_qrcode,
            servicePhones: isLegalData(services_phone),
          },
        });
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，联系客服，修改添加客服信息
    *editServiceInfo({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editServiceInfo, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success('修改成功！');
        __dva_resolve();
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，获取需要解绑平台的二维码
    *getUnBindQrCode({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getUnbindQrCode, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      const { url, sceneId } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            unBindQrcode: url,
            unBindSceneId: sceneId,
          },
        });
        __dva_resolve({ qrcodeUrl: url, unBindSceneId: sceneId });
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，检查用户解绑的操作结果
    *checkUnBindScan({ payload, __dva_resolve }, { call }) {
      const response = yield call(checkUnBindRecord, payload);
      if (!response) return;
      __dva_resolve(response);
    },
    // 微信管理，设置，自定义菜单，获取自定义菜单
    *getMenuList({ __dva_resolve }, { call, put, select }) {
      // 每次重新生成新的初始数据，防止数据缓存
      const initMenuList = Array.from({ length: 3 }).map((_, index) => {
        const sub_button = Array.from({ length: 5 }).map((__, _index) => ({
          name: `${index + 1}${5 - _index}`,
          menuKey: `${index + 1}${5 - _index}`,
        }));
        return {
          name: `${index}`,
          menuKey: `${index}`,
          sub_button,
        };
      });
      const { platformList = {} } = yield select(_ => _.platform);

      const { weixin = {} } = platformList;
      const { platform } = weixin;
      const response = yield call(getMenuList, { platform });
      if (!response) return;
      const { code, msg, data = {} } = response;
      const { menu = {} } = data;
      const { button = [] } = menu;
      if (code == 0) {
        if (isArray(button)) {
          button.forEach((val = {}, index) => {
            const {
              name,
              url,
              appid,
              key,
              key_word_content,
              type,
              page,
              sub_button = [],
              pagepath,
            } = val;
            sub_button.forEach((item, _index) => {
              item.menuKey = `${index + 1}${sub_button.length - _index}`; // 倒序
            });
            initMenuList[index].name = name;
            initMenuList[index].url = url;
            initMenuList[index].appid = appid;
            initMenuList[index].key_word_content = key_word_content;
            initMenuList[index].key = key;
            initMenuList[index].type = type;
            initMenuList[index].page = page;
            initMenuList[index].pagepath = pagepath;
            initMenuList[index].sub_button.splice(
              5 - sub_button.length,
              sub_button.length,
              ...sub_button,
            );
          });
        }
        yield put({
          type: 'save',
          payload: {
            updatingMenu: false,
            menuButtonList: isLegalData(initMenuList),
            oldMenuButtonList: isLegalData(cloneDeep(initMenuList)), // 用于对比，防止重复发布
          },
        });
      } else {
        message.error(msg);
      }
      __dva_resolve(isLegalData(initMenuList));
    },
    // 微信管理，设置，自定义菜单，更新本地选中的菜单
    *updateMenuList({ payload, __dva_resolve }, { put, select }) {
      const { menuButtonList = [], selectMenuData = {} } = yield select(_ => _.wxSettings);
      const { changedMenu = {} } = payload;

      menuButtonList.forEach((val, index) => {
        const { sub_button, menuKey } = val;
        if (menuKey == selectMenuData.menuKey) {
          menuButtonList[index] = {
            ...changedMenu,
            menuKey: selectMenuData.menuKey,
            sub_button,
          };
          return;
        }
        sub_button.forEach((subMenu, subIndex) => {
          if (subMenu.menuKey == selectMenuData.menuKey) {
            menuButtonList[index].sub_button[subIndex] = {
              menuKey: selectMenuData.menuKey,
              ...changedMenu,
              sub_button: [],
            };
          }
        });
      });

      yield put({
        type: 'save',
        payload: {
          menuButtonList,
        },
      });

      __dva_resolve();
    },

    // 微信管理，设置，自定义菜单，获取官方提供的公众号菜单
    *getOfficialMenu({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOfficialMenu, payload);
      if (!response) return;
      const { code, data = [] } = response;
      const officialMenuMap = {};

      if (isArray(data)) {
        data.forEach(({ name, key }) => {
          officialMenuMap[key] = name;
        });
      }

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            officialMenuList: isLegalData(data),
            officialMenuMap,
          },
        });
        __dva_resolve(isLegalData(data));
      } else {
        __dva_reject();
      }
    },
    // 微信管理，设置，自定义菜单，获取官方提供的公众号菜单
    *getMiniProgramMenu({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(getMiniProgramMenu, payload);
      if (!response) return;
      const { code, data = {} } = response;
      const { platform_info = {}, menu_list = [] } = data;
      const miniProgramMenuMap = {};

      if (isArray(menu_list)) {
        menu_list.forEach(({ name, page }) => {
          miniProgramMenuMap[page] = name;
        });
      }

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            miniProgramMenuList: isLegalData(menu_list),
            currentMiniAppPlatform: platform_info,
            miniProgramMenuMap,
          },
        });
        __dva_resolve(isLegalData(data));
      }
    },
    // 微信管理，设置，自定义菜单，当用户选择同城急送内置链接时，检查用户是否开通此功能
    *checkHasOpenTCJS({ payload, __dva_resolve }, { call }) {
      const { selectMenuKey, platform } = payload;
      if (selectMenuKey === 'urgent_delivery') {
        const response = yield call(checkHasOpenTCJS, { platform });
        if (!response) return;
        const { code, msg, data = {} } = response;
        const { urgent_delivery } = data;
        if (code == 0) {
          if (urgent_delivery == 1) {
            __dva_resolve(true);
          } else {
            __dva_resolve(false);
          }
        } else {
          message.error(msg);
        }
      } else {
        __dva_resolve(true);
      }
    },
    // 微信管理，设置，自定义菜单，发布菜单
    *publishMenu({ __dva_resolve, __dva_reject }, { call, select }) {
      const { menuButtonList = [], oldMenuButtonList = [] } = yield select(_ => _.wxSettings);
      const { platformList = {} } = yield select(_ => _.platform);
      const { weixin: { platform } = {} } = platformList;

      if (isEqual(menuButtonList, oldMenuButtonList)) {
        message.warning('菜单未修改，请修改后再发布');
        return;
      }
      const payload = {};
      menuButtonList
        .filter(i => i.name != 0 && i.name != 1 && i.name != 2)
        .forEach((menu = {}, index) => {
          // 解构，删除多余参数
          const { sub_button = [], menuKey: m_key, systemMenuKey: s_key, ...rest } = menu;
          // 有效的子菜单，删除了前端自带的数据
          const real_sub_button = sub_button.filter(val => val.type);

          if (real_sub_button.length > 0) {
            // 当有子菜单时，主菜单只需要name字段
            payload[index + 1] = { first_name: rest.name };
            real_sub_button.forEach(({ menuKey, systemMenuKey, ...rests }, subIndex) => {
              payload[index + 1][subIndex + 1] = {
                ...rests,
                ...(systemMenuKey ? { url: systemMenuKey } : {}), // 系统菜单参数
              };
            });
          } else if (rest.type) {
            // 当只设置主菜单时，保留所有字段
            payload[index + 1] = {
              1: {
                ...rest,
                ...(s_key ? { url: s_key } : {}), // 主菜单，系统菜单参数
              },
            };
          }
        });
      const response = yield call(publishMenu, { menu: JSON.stringify(payload), platform });
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve(code);
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
    // 微信管理，设置，自定义菜单，复制系统菜单连接
    *copySystemURL({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(copySystemURL, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            systemMenuURL: data,
          },
        });
        __dva_resolve(data);
      } else {
        yield put({
          type: 'save',
          payload: {
            systemMenuURL: '',
          },
        });
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，收益广告展示位置查询
    *getRevenueAdvShowPosition({ __dva_resolve }, { call, put }) {
      const response = yield call(getRevenueAdvShowPosition);
      if (!response) return;
      const { code, msg, data = {} } = response;

      if (code == 0) {
        const res = isLegalData(data, {});
        const checkedList = Object.keys(res).filter(key => res[key] == 1);
        yield put({
          type: 'save',
          payload: {
            revenueCheckedList: isLegalData(checkedList),
          },
        });
        __dva_resolve(checkedList);
      } else {
        message.error(msg);
      }
    },
    // 微信管理，设置，收益广告展示位置设置
    *setRevenueAdvShowPosition({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(setRevenueAdvShowPosition, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve();
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，小程序推广，获取推广设置
    *getPromoteMiniProgram({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getPromoteMiniProgram, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      const keys = ['qrcode_status', 'template_status', 'reply_status'];

      if (code == 0) {
        const res = isLegalData(data, {});
        const checkedList = Object.keys(res).filter(key => keys.includes(key) && res[key] == 2);
        __dva_resolve(checkedList);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微信管理，设置，小程序推广，编辑推广设置
    *editPromoteMiniProgram({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editPromoteMiniProgram, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve();
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
